#!/usr/bin/env bash

if [ `whoami` != "root" ];then
  echo "must be root"
  exit 1
fi

which "docker" > /dev/null
if [ $? -eq 0 ]
then
    echo "docker已安装"
    exit 1
fi

echo "安装docker..."
mkdir -p /etc/docker

cat <<EOF > /etc/docker/daemon.json
{
"registry-mirrors":[
    "https://hub-mirror.c.163.com",
    "https://registry.aliyuncs.com",
    "http://7e61f7f9.m.daocloud.io",
    "https://docker.mirrors.ustc.edu.cn",
    "https://registry.docker-cn.com",
    "https://reg-mirror.qiniu.com"
],
"graph":"/var/lib/docker",
"insecure-registries":["localhost:8000"],
"features": { "buildkit": true }
}
EOF

wget -q -O - https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get clean all
apt-get update
version=$(apt-cache madison docker-ce | cut -d '|' -f 2 | sed 's/^[ ]*//g' | awk 'BEGIN {max = 0} {if ($1+0 > max+0) max=$1} END {print max}')
echo "docker 最新版：${version}"
apt-get install -y docker-ce=$version


echo "开机启动docker..."
systemctl enable docker


# 设置存储位置，需要提前执行以下命令
# mkdir -p /data/docker
# ln -s /data/docker /var/lib/docker
